package com.dropgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;
import com.dropgame.tools.ImageButton;
import com.dropgame.tools.TransitionEffect;

public class FreePlayArrowScreen implements Screen{

	private DropGame game;
	private OrthographicCamera guiCam;
	
	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
    
    public Rectangle viewport;
    
    private Input input;
    
	private SpriteBatch batcher;
	private TransitionEffect transitionEffect;
	
	private Sprite savanah;
	
	
	private ImageButton playButton;
	private ImageButton upLevelButton;
	private ImageButton downLevelButton;
	private ImageButton upBackgroundButton;
	private ImageButton downBackGroundButton;
	
	private int level;
	
	private BitmapFont font;
	
	
	public FreePlayArrowScreen(DropGame game){
		this.game = game;
		this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);		
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		this.batcher = new SpriteBatch();
		this.viewport = new Rectangle(0.0f, 0.0f, 0.0f, 0.0f);
		
		this.input = Gdx.input;
		
		font = new BitmapFont();
		font.setScale(2.0f);
		
		transitionEffect = new TransitionEffect(game);
		
		this.savanah = AssetLoader.savanah;
		
		//level = (int) (game.getProfileService().retrieveProfile().volume * 10);
		
		Sprite[] upArray= {AssetLoader.upArrow};
		Sprite[] downArray = {AssetLoader.downArrow};
		Sprite[] playArray = {AssetLoader.playButton1, AssetLoader.playButton3, AssetLoader.playButton5};
		
		this.upLevelButton = new ImageButton(0.1f, upArray, input, guiCam, viewport, 10.0f, 350.0f);
		this.downLevelButton = new ImageButton(0.1f, downArray, input, guiCam, viewport, 10.0f, 250.0f);
		this.playButton = new ImageButton(0.15f, playArray, input, guiCam, viewport, 85.0f, 75.0f);
		
		level = 1;
		
	}
	
	@Override
	public void render(float deltaTime) {
		GL10 gl = Gdx.app.getGraphics().getGL10();
		
		guiCam.update();
		guiCam.apply(gl);
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
          	  (int) viewport.width, (int) viewport.height);
		
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		
		batcher.begin();
		savanah.draw(batcher);
		upLevelButton.getKeyFrame(deltaTime).draw(batcher);
		downLevelButton.getKeyFrame(deltaTime).draw(batcher);
		playButton.getKeyFrame(deltaTime).draw(batcher);
		font.draw(batcher, Integer.toString(level), 72.0f, 330.0f);
		batcher.end();
		
		transitionEffect.movingScreen(batcher, deltaTime);	

		updateLogic(deltaTime);
	}
	
	public void updateLogic(float deltaTime){
		if(upLevelButton.getCompleted()){
			if(level >= 10) level = 10;
			else level++;
			
		}
		
		if(downLevelButton.getCompleted()){
			if(level <= 1) level = 1;
			else level--;
			
		}
		
		if(playButton.getTouched()){
			upLevelButton.setAcceptInput(false);
			downLevelButton.setAcceptInput(false);
		} else{upLevelButton.setAcceptInput(true);
			   downLevelButton.setAcceptInput(true);
			   }
		
		if(playButton.getCompleted()){
			transitionEffect.nextScreen(new GameScreen(game, level));
		}
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        //viewport = new Rectangle(crop.x, crop.y, w, h);
        viewport.set(crop.x, crop.y, w, h);
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

}
